using Microsoft.AspNetCore.Mvc;
using ERPBackend.Models;
using ERPBackend.Services;
using System.Threading.Tasks;
using System;

namespace ERPBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class PurchaseStockInsController : ControllerBase
    {
        private readonly IPurchaseStockInService _purchaseStockInService;

        public PurchaseStockInsController(IPurchaseStockInService purchaseStockInService)
        {
            _purchaseStockInService = purchaseStockInService;
        }

        // GET: api/PurchaseStockIns
        [HttpGet]
        public async Task<ActionResult<object>> GetPurchaseStockIns(int page = 1, int pageSize = 10)
        {
            var total = await _purchaseStockInService.GetTotalPurchaseStockInsAsync();
            var stockIns = await _purchaseStockInService.GetPurchaseStockInsPagedAsync(page, pageSize);
            return Ok(new { items = stockIns, total });
        }

        // GET: api/PurchaseStockIns/5
        [HttpGet("{id}")]
        public async Task<ActionResult<PurchaseStockIn>> GetPurchaseStockIn(int id)
        {
            var stockIn = await _purchaseStockInService.GetPurchaseStockInByIdAsync(id);

            if (stockIn == null)
            {
                return NotFound();
            }

            return stockIn;
        }

        // GET: api/PurchaseStockIns/code/{stockInCode}
        [HttpGet("code/{stockInCode}")]
        public async Task<ActionResult<PurchaseStockIn>> GetPurchaseStockInByCode(string stockInCode)
        {
            var stockIn = await _purchaseStockInService.GetPurchaseStockInByCodeAsync(stockInCode);

            if (stockIn == null)
            {
                return NotFound();
            }

            return stockIn;
        }

        // PUT: api/PurchaseStockIns/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutPurchaseStockIn(int id, PurchaseStockIn stockIn)
        {
            if (id != stockIn.Id)
            {
                return BadRequest();
            }

            try
            {
                var updated = await _purchaseStockInService.UpdatePurchaseStockInAsync(stockIn);
                if (!updated)
                {
                    return NotFound();
                }
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }

            return NoContent();
        }

        // POST: api/PurchaseStockIns
        [HttpPost]
        public async Task<ActionResult<PurchaseStockIn>> PostPurchaseStockIn(PurchaseStockIn stockIn)
        {
            try
            {
                var createdStockIn = await _purchaseStockInService.CreatePurchaseStockInAsync(stockIn);
                return CreatedAtAction("GetPurchaseStockIn", new { id = createdStockIn.Id }, createdStockIn);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // DELETE: api/PurchaseStockIns/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeletePurchaseStockIn(int id)
        {
            var deleted = await _purchaseStockInService.DeletePurchaseStockInAsync(id);
            if (!deleted)
            {
                return NotFound();
            }

            return NoContent();
        }

        // PATCH: api/PurchaseStockIns/5/status
        [HttpPatch("{id}/status")]
        public async Task<IActionResult> ChangePurchaseStockInStatus(int id, [FromBody] string status)
        {
            var success = await _purchaseStockInService.ChangePurchaseStockInStatusAsync(id, status);
            if (!success)
            {
                return NotFound();
            }

            return NoContent();
        }

        // PATCH: api/PurchaseStockIns/5/approve
    [HttpPatch("{id}/approve")]
    public async Task<IActionResult> ApprovePurchaseStockIn(int id, [FromBody] PurchaseStockInApproveRequest request)
    {
        try
        {
            if (!int.TryParse(request.ApprovedBy, out int approverId))
            {
                return BadRequest("审批人ID必须是有效的整数");
            }
            var success = await _purchaseStockInService.ApprovePurchaseStockInAsync(id, approverId, request.ApprovalRemark);
            if (!success)
            {
                return NotFound();
            }
            return NoContent();
        }
        catch (ArgumentException ex)
        {
            return BadRequest(ex.Message);
        }
        catch (InvalidOperationException ex)
        {
            return BadRequest(ex.Message);
        }
    }

    public class PurchaseStockInApproveRequest
    {
        public string ApprovedBy { get; set; }
        public string ApprovalRemark { get; set; }
    }
}

  
}